package b3d.parsers
{
	import b3d.geometry.Mesh3D;
	
	import flash.net.FileReference;
	import flash.net.registerClassAlias;
	import flash.utils.ByteArray;

	public class Serialize_b3d
	{
		public static function serialize(mesh:Mesh3D):void
		{
			registerClassAlias("Mesh3D", Mesh3D);
			
			var data:ByteArray = new ByteArray();
			data.writeObject(mesh);
			data.compress();
			
			var f:FileReference = new FileReference();
			f.save(data, "mesh.b3d");
		}
		public static function serializex(mesh:Mesh3D):void
		{
			var data:ByteArray = new ByteArray();
			var i:int;
			
			var verts:Vector.<Number> = mesh.vertices;
			var numVerts:int = verts.length;
			
			data.writeInt(numVerts);
			while(i < numVerts)
			{
				data.writeFloat(verts[i]);
				i++;
			}
			
			i = 0;			
			var uvs:Vector.<Number> = mesh.uvs;
			var numUvs:int = uvs.length;
			
			data.writeInt(numUvs);
			while(i < numUvs)
			{
				data.writeFloat(uvs[i]);
				i++;
			}
			
			i = 0;			
			var normals:Vector.<Number> = mesh.normals;
			var numNormals:int = normals.length;
			
			data.writeInt(numNormals);
			while(i < numNormals)
			{
				data.writeFloat(normals[i]);
				i++;
			}
			
			i = 0;			
			var ids:Vector.<uint> = mesh.ids;
			var numIds:int = ids.length;
			
			data.writeInt(numIds);
			while(i < numIds)
			{
				data.writeUnsignedInt(ids[i]);
				i++;
			}
			
			data.compress();
			
			var f:FileReference = new FileReference();
			f.save(data, "mesh.b3dx");
		}
	}
}